package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.model.*;
import com.jiyun.service.CatalogCourseService;
import com.jiyun.service.CatalogService;
import com.jiyun.service.CourseService;
import com.jiyun.service.TrainService;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 朱庆辉
 * @Date 2023/7/28 16:05
 * @Email 1018276870@qq.com
 */
@RestController
@RequestMapping("catalog_course")
public class CatalogCourseController {
    @Autowired
    private CatalogCourseService catalogCourseService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("updateStatus")
    public R updateStatus(Integer id){
        Info o = (Info) redisTemplate.opsForValue().get(1);
        QueryWrapper<CatalogCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_name",o.getName()).eq("catalog_id",id);
        List<CatalogCourse> list = catalogCourseService.list(queryWrapper);
        if (list.size()!=0){
            return R.ok("该课程已学习");
        }
        CatalogCourse catalogCourse = new CatalogCourse();
        catalogCourse.setCatalogId(id);
        catalogCourse.setCourseName(o.getName());
        catalogCourseService.save(catalogCourse);
        return R.ok("学习成功");
    }

    @Autowired
    private CatalogService catalogService;
    @GetMapping("findAllCatalogNum")
    public float findAllCatalogNum(){
        Info o = (Info) redisTemplate.opsForValue().get(1);
        QueryWrapper<CatalogCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_name",o.getName());
        List<CatalogCourse> list = catalogCourseService.list(queryWrapper);
        CatalogDto catalogDto = new CatalogDto();
        List<Catalog> all = catalogService.findAll(catalogDto);
        float sum = 0;
        float sum1 = list.size();
        for (Catalog catalog : all) {
            sum  += catalog.getChildren().size();
        }
        float num = sum1/sum;

        return num;
    }

    @Autowired
    private TrainService trainService;

    @Autowired
    private CourseService courseService;
    @RequestMapping("findByPercent")
    public List<CourseInfo> findByPercent(Integer id){
        List<TrainPercent> byPercent = trainService.findByPercent(id);
        List<CourseInfo> courseInfos = new ArrayList<>();
        for (TrainPercent trainPercent : byPercent) {
            Course course = courseService.findById(trainPercent.getCourseId());
            QueryWrapper<CatalogCourse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("course_name",course.getName());
            List<CatalogCourse> list = catalogCourseService.list(queryWrapper);
            CatalogDto catalogDto = new CatalogDto();
            List<Catalog> all = catalogService.findAll(catalogDto);
            float sum = 0;
            float sum1 = list.size();
            for (Catalog catalog : all) {
                sum  += catalog.getChildren().size();
            }
            float num = sum1/sum;
            CourseInfo courseInfo = new CourseInfo();
            courseInfo.setName(course.getName());
            courseInfo.setGnum(course.getGnum());
            courseInfo.setStudy(course.getStudy());
            courseInfo.setPercent(num*100);
            courseInfos.add(courseInfo);
        }
        return courseInfos;
    }
}
